package com.shen.eduservice.controller.front;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shen.commonutils.JwtUtils;
import com.shen.commonutils.Result;
import com.shen.commonutils.ordervo.CourseWebVoOrder;
import com.shen.eduservice.client.OrdersClient;
import com.shen.eduservice.entity.EduCourse;
import com.shen.eduservice.entity.EduTeacher;
import com.shen.eduservice.entity.chapter.ChapterVo;
import com.shen.eduservice.entity.frontvo.CourseFrontVo;
import com.shen.eduservice.entity.vo.CourseWebVo;
import com.shen.eduservice.service.EduChapterService;
import com.shen.eduservice.service.EduCourseService;
import com.shen.eduservice.service.EduTeacherService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/eduservice/coursefront")
@CrossOrigin
public class CourseFrontController {


    @Autowired
    EduCourseService eduCourseService;
    @Autowired
    EduChapterService eduChapterService;
    @Autowired
    OrdersClient ordersClient;
    //查询前8条课程信息 前4条讲师信息
    @PostMapping("getFrontCourseList/{page}/{limit}")
    public Result getFrontCourseList(@PathVariable long page, @PathVariable long limit,

                                     @RequestBody(required = false) CourseFrontVo courseFrontVo){
        System.out.println("-------------------> ");
        System.out.println(courseFrontVo);
        Page<EduCourse> coursePage=new Page<>(page,limit);

        Map<String,Object> map=eduCourseService.getCourseFrontList(coursePage,courseFrontVo);
        return  Result.ok().data("data",map);
    }

    // 根据课程id 得到课程信息
    @GetMapping("getFrontCourseInfo/{courseId}")
    public  Result getFrontCourseInfo(@PathVariable String courseId, HttpServletRequest request){

            System.out.println("courseId-----------------"+courseId);
        //查询课程信息
        CourseWebVo courseWebVo=eduCourseService.getBaseCourseInfo(courseId);
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        System.out.println("memberId--------->"+memberId);
        //根据课程id查询 章节和小节信息
        List<ChapterVo> chapterVideoList = eduChapterService.getChapterVideoByCourseId(courseId);
        if(!StringUtils.isEmpty(memberId)){
            boolean status = ordersClient.getStatus(courseId, memberId);
            return Result.ok().data("courseWebVo",courseWebVo).data("chapterVideoList",chapterVideoList).data("isBuy",status);
        }



        else  return Result.ok().data("courseWebVo",courseWebVo).data("chapterVideoList",chapterVideoList);
    }

    @PostMapping("getCourseInfoOrder/{id}")
    public CourseWebVoOrder getCourseInfoOrder(@PathVariable String id){

        CourseWebVo baseCourseInfo = eduCourseService.getBaseCourseInfo(id);
        CourseWebVoOrder courseWebVoOrder=new CourseWebVoOrder();
        BeanUtils.copyProperties(baseCourseInfo,courseWebVoOrder);


        return courseWebVoOrder;
    }
}
